package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityForgetToInputPwdBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.ToastUtil;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/02/28 12:39
 */

public class ForgetToInputPwdActivity extends BaseActivity{

    ActivityForgetToInputPwdBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_forget_to_input_pwd);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setTitleText("重置密码");
        setBack();
        binding.tvModuleUpdata.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tvModuleUpdata:
                String phone = getIntent().getStringExtra("phone");
                String code = getIntent().getStringExtra("code");
                String pwd = binding.pwd.getText().toString().trim();
                String pwdAgain = binding.pwdAgain.getText().toString().trim();
                if (TextUtils.isEmpty(pwd) || pwd.length() < 6) {
                    showInCommonDialog("字母 + 数组组成密码更安全");
                    binding.pwd.setText("");
                    return;
                }

                if (TextUtils.isEmpty(pwdAgain) || !pwdAgain.equals(pwd)) {
                    showInCommonDialog("两次输入密码不一致");
                    binding.pwdAgain.setText("");
                    return;
                }

                HTTPAPI.getInstance().updataPwd(phone, pwd, code, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        ToastUtil.showToast("修改成功，请重新登录");
                        startActivity(new Intent(ForgetToInputPwdActivity.this,LoginActivity.class));
                        finish();
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        LogUtil.e("提交失败"+ e.getMessage());
                    }
                });

                break;
                default:
        }
    }
}
